//
// MenuManager.cpp for  in /home/gomes_m//svn/google/ticket-pour-gdl/menu_V1
// 
// Made by mickael gomes
// Login   <gomes_m@epitech.net>
// 
// Started on  Wed May 15 09:28:00 2013 mickael gomes
// Last update Thu May 30 10:35:16 2013 mickael gomes
//

#include		<cstdlib>
#include		"NewGame.hh"
#include		"Continue.hh"
#include		"MenuManager.hh"
#include		"Menu.hh"
#include		"Options.hh"
#include		"Vector3f.hh"

void			MenuManager::initialize(void)
{
  window_.create();
  tab_.push_back(new Menu);
  tab_.push_back(new NewGame);
  tab_.push_back(new Continue);
  tab_.push_back(new Options);
  tab_[0]->initialize();
  tab_[1]->initialize();
  tab_[2]->initialize();
  tab_[3]->initialize();
  camera_.initialize();
  cursor_.initialize("images/cursor.png");
  ftab_.push_back(std::pair <gdl::Keys::Key, Key>(gdl::Keys::B, B));
  ftab_.push_back(std::pair <gdl::Keys::Key, Key>(gdl::Keys::Up, UP));
  ftab_.push_back(std::pair <gdl::Keys::Key, Key>(gdl::Keys::Down, DOWN));
  ftab_.push_back(std::pair <gdl::Keys::Key, Key>(gdl::Keys::Left, LEFT));
  ftab_.push_back(std::pair <gdl::Keys::Key, Key>(gdl::Keys::Right, RIGHT));
  ftab_.push_back(std::pair <gdl::Keys::Key, Key>(gdl::Keys::Return, RETURN));
  tab_[0]->resetPos(cursor_.coord);
  choice_ = 0;
}

void			MenuManager::update()
{
  std::vector< std::pair<gdl::Keys::Key, Key> >::iterator it = ftab_.begin();

  while (it != ftab_.end() && input_.isKeyDown(it->first) == false)
    it++;
  if (it != ftab_.end())
    tab_[choice_]->update(it->second, cursor_);
  if (choice_ != cursor_.getChoice())
    {  
      choice_ = cursor_.getChoice();
      if (choice_ == 4)
	unload();
      tab_[choice_]->resetPos(cursor_.coord);
    }
}

void			MenuManager::draw(void)
{
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glClearColor(0.00f, 0.00f, 0.00f, 0.0f);
  glClearDepth(1.0f);

  tab_[choice_]->draw();
  cursor_.draw();
  this->window_.display();
}

void			MenuManager::unload()
{
  exit(EXIT_SUCCESS);
}
